<template>
  <div class="theme-manage w-full h-full">
    <div class="btns w-full">
      <a-button @click="themeAdd" type="primary">添加</a-button>
      <a-button @click="themeDelete" type="primary" :disabled="table.rowSelection.selectedRowKeys.length<1">删除</a-button>
    </div>
    <a-table
      style="height:calc(100% - 40px);overflow-y:auto;"
      :rowKey="record => record.id"
      :columns="table.columns"
      :row-selection="table.rowSelection"
      :data-source="table.dataSource"
      :pagination="table.pagination"
      bordered
    >
      <template slot="type" slot-scope="text">
        <span>{{ showType(text) }}</span>
      </template>
    </a-table>
  </div>
</template>

<script>
import orupService from '@/api/orupService'
import moment from 'moment'

const columns = [
  {
    title: '主题名称',
    align: 'center',
    dataIndex: 'topicName'
  },
  {
    title: '创建时间',
    align: 'center',
    dataIndex: 'createTime',
    customRender: function (text, row, index) {
      return moment(text).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  {
    title: '主题类型',
    align: 'center',
    dataIndex: 'topicType',
    scopedSlots: { customRender: 'type' }
  },
  {
    title: '读队列数',
    align: 'center',
    dataIndex: 'readQueueNums'
  },
  {
    title: '写队列数',
    align: 'center',
    dataIndex: 'writeQueueNums'
  },
  {
    title: 'perm',
    align: 'center',
    dataIndex: 'perm'
  },
  {
    title: '是否有序',
    align: 'center',
    dataIndex: 'order',
    customRender: (text) => {
      return text ? '是' : '否'
    }
  }
]

export default {
  name: 'ThemeManage',

  data () {
    return {
      typeList: [],
      filter: {
        filterText: '',
        pageIndex: 1,
        pageSize: 10
      },
      table: {
        columns: columns,
        dataSource: [],
        rowSelection: {
          selectedRowKeys: [],
          selectedRow: {},
          onChange: (selectedRowKeys, selectedRows) => {
            this.table.rowSelection.selectedRowKeys = selectedRowKeys.length ? [selectedRowKeys.pop()] : []
            this.table.rowSelection.selectedRow = selectedRows.length ? selectedRows.pop() : {}
          }
        },
        pagination: {
          pageSize: 10,
          total: 0,
          current: 1,
          showSizeChanger: true,
          pageSizeOptions: ['10', '20', '50', '100', '200', '500'],
          showTotal: (total) => `共${total}条数据`,
          onShowSizeChange: (current, pageSize) => {
            this.filter.pageIndex = 1
            this.filter.pageSize = pageSize
          },
          onChange: (current, pageSize) => {
            this.filter.pageIndex = current
            this.filter.pageSize = pageSize
            this.table.pagination.current = current
          }
        }
      }
    }
  },

  async mounted () {
    await this.getTypeList()
    this.loadData()
  },

  methods: {
    moment,
    async loadData () {
      this.table.dataSource = await orupService.topicQuery()
    },
    async getTypeList () {
      this.typeList = await orupService.topicTypeListQuery()
    },
    showType (type) {
      return this.typeList.filter(item => {
        return item.type === type
      })[0].typeName
    },
    themeAdd () {
      const _this = this
      this.$dialog({
        title: '新增主题',
        content: () => import('@v/investmentOnline/messageCenterManage/themeDialog/ThemeAddDialog.vue'),
        width: 600,
        bodyStyle: { height: '500px' },
        centered: true,
        maskClosable: false,
        on: {
          async ok (ctl) {
            const dialogCtl = ctl.getDialogCtl()
            const valid = dialogCtl.submit()
            if (valid) {
              orupService.topicSave(dialogCtl.form).then(res => {
                if (res && res.id) {
                  _this.$message.success('添加成功!')
                  _this.loadData()
                }
              })
              ctl.closeDialog()
            }
          }
        }
      })
    },
    themeDelete () {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '您确定删除所选记录么？',
        okType: 'danger',
        onOk () {
          orupService.topicDelete(_this.table.rowSelection.selectedRow[0]).then(res => {
            if (res) {
              _this.$message.success('删除成功!')
              _this.loadData()
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.theme-manage {
  background: #fff;
  padding: 10px;
  .btns {
    height: 40px;
  }
}

</style>
